package com.stox.client.ui.javafx.widget;

import java.util.HashSet;
import java.util.Set;

import javafx.scene.control.ContextMenu;

public final class ContextMenuConfigurerRegistry {

	private static ContextMenuConfigurerRegistry instance;
	
	public static ContextMenuConfigurerRegistry getInstance(){
		if(null == instance){
			instance = new ContextMenuConfigurerRegistry();
		}
		return instance;
	}
	
	private Set<ContextMenuConfigurer> contextMenuConfigurers = new HashSet<>();
	private Set<ContextMenu> contextMenus = new HashSet<>();
	
	private ContextMenuConfigurerRegistry() {
		// Singleton
	}
	
	public void attachContextMenu(ContextMenu contextMenu){
		contextMenus.add(contextMenu);
		for(ContextMenuConfigurer contextMenuConfigurer : contextMenuConfigurers){
			contextMenuConfigurer.configure(contextMenu);
		}
	}
	
	public void detachContextMenu(ContextMenu contextMenu){
		for(ContextMenuConfigurer contextMenuConfigurer : contextMenuConfigurers){
			contextMenuConfigurer.clear(contextMenu);
		}
		contextMenus.remove(contextMenu);
	}
	
	public void addContextMenuConfigurer(ContextMenuConfigurer contextMenuConfigurer){
		if(null != contextMenuConfigurer && !contextMenuConfigurers.contains(contextMenuConfigurer)){
			contextMenuConfigurers.add(contextMenuConfigurer);
			for(ContextMenu contextMenu : contextMenus){
				contextMenuConfigurer.configure(contextMenu);
			}
		}
	}
	
	public void removeContextMenuConfigurer(ContextMenuConfigurer contextMenuConfigurer){
		if(contextMenuConfigurers.contains(contextMenuConfigurer)){
			contextMenuConfigurers.remove(contextMenuConfigurer);
			for(ContextMenu contextMenu : contextMenus){
				contextMenuConfigurer.clear(contextMenu);
			}
		}
	}
	
}
